package com.sunkingdom.qq.user.controller.impl;

import com.sunkingdom.qq.global.Result;
import com.sunkingdom.qq.user.controller.UserInfoController;
import com.sunkingdom.qq.user.entity.User;
import com.sunkingdom.qq.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/info")
public class UserinfoControllerImpl implements UserInfoController {
	private static final String USER_INFO = "user/user-info";
	private static final String COMPANION_INFO = "user/companion-info";
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
	public String getUserInfo(@PathVariable("userId") Long userId,
			HttpServletRequest request, HttpServletResponse response) {
		User user = userService.getUserById(userId);
		request.setAttribute("user", user);
		return USER_INFO;
	}

	@RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
	public String deleteUserByUserId(@PathVariable("userId") Long userId,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			userService.deleteUserById(userId);
			request.setAttribute(Result.MESSAGE, Result.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute(Result.MESSAGE, Result.FAILURE);
		}
		return Result.RESULT;
	}

	@RequestMapping(value = "/companion/{userId}", method = RequestMethod.GET)
	public String getCompanionInfo(@PathVariable("userId") Long userId,
			HttpServletRequest request, HttpServletResponse response) {
		User user = userService.getUserById(userId);
		request.setAttribute("user", user);
		return COMPANION_INFO;
	}

}
